package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-02
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper; // 假设存在该 Mapper 接口
    /**
     * 查询详细护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
@Override
public NursingPlanVo selectNursingPlanById(Long id) {
    NursingPlan plan = getById(id);
    NursingPlanVo planVo = new NursingPlanVo();
    // 修复：正确顺序应该是 source, target
    BeanUtils.copyProperties(plan, planVo);

    LambdaQueryWrapper<NursingProjectPlan> qw = new LambdaQueryWrapper<>();
    qw.eq(NursingProjectPlan::getPlanId, id);
    List<NursingProjectPlanVo> voList = nursingProjectPlanMapper.selectList(qw).stream().map(projectPlan -> {
        NursingProjectPlanVo projectPlanVo = new NursingProjectPlanVo();
        // 修复：正确顺序应该是 source, target
        BeanUtils.copyProperties(projectPlan, projectPlanVo);
        projectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
        projectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
        return projectPlanVo;
    }).collect(Collectors.toList());

    planVo.setProjectPlans(voList);
    return planVo;
}


    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
@Override
@Transactional
public int insertNursingPlan(NursingPlanDto dto) {
    // 使用工具类实现dto 转换为 entity
    NursingPlan nursingPlan = new NursingPlan();
    BeanUtils.copyProperties(dto, nursingPlan); // 修正参数顺序
    int num = this.nursingPlanMapper.insertNursingPlan(nursingPlan);

    // 新增护理计划和项目关联
    List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
            this.nursingProjectPlanMapper.insert(projectPlan);
        }
    return num;
}


    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);  // 正确：从 DTO 拷贝到实体对象

        int num = this.nursingPlanMapper.updateById(nursingPlan);

        //先删除，再新增
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            //根据检查计划id，删除旧数据
            this.nursingProjectPlanMapper.delete(
                    new LambdaQueryWrapper<NursingProjectPlan>()
                            .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            //新增
            for (NursingProjectPlan projectPlan : projectPlans){
                projectPlan.setPlanId(nursingPlan.getId());
                this.nursingProjectPlanMapper.insert(projectPlan);
            }
        }
        return num;
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
@Transactional(rollbackFor = Exception.class)
public int deleteNursingPlanByIds(Long id)
{
    // 删除护理计划与护理项目的关系
    int projectPlanResult = nursingProjectPlanMapper.deleteById(id);
    // 删除护理计划
    int planResult = nursingPlanMapper.deleteById(id);

    // 返回实际删除的护理计划记录数
    return planResult;
}


    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
                return removeById(id) ? 1 : 0;
    }
}
